package com.minisoft.utils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author JueSu
 * @description: TODO
 * @date 2024/2/4 11:00
 */
public class DateUtil {
    public static LocalDate[][] generateDateRange(int year, int intervalDays) {
        List<LocalDate[]> dateRanges = new ArrayList<>();
        LocalDate startDate = LocalDate.of(year, 1, 1);
        LocalDate endDate = LocalDate.of(year + 1, 1, 1).plusDays(-1); // 假设我们想要包含整年

        while (startDate.isBefore(endDate)) {
            LocalDate endDateInRange = startDate.plusDays(intervalDays - 1);
            if (endDateInRange.isAfter(endDate)) {
                endDateInRange = endDate;
            }
            dateRanges.add(new LocalDate[]{startDate, endDateInRange});
            startDate = startDate.plusDays(intervalDays);
        }

        return dateRanges.toArray(new LocalDate[0][]);
    }

    public static LocalDate[][] generateDateRange(int year, int mon,int intervalDays) {
        return generateDateRange(year,mon,1,intervalDays);
    }

    public static LocalDate[][] generateDateRange(int year, int mon,int intervalMonths,int intervalDays) {
        List<LocalDate[]> dateRanges = new ArrayList<>();
        LocalDate startDate = LocalDate.of(year, mon, 1);
        LocalDate endDate = startDate.plusMonths(intervalMonths).plusDays(-1);
        while (startDate.isBefore(endDate)) {
            LocalDate endDateInRange = startDate.plusDays(intervalDays - 1);
            if (!endDateInRange.isBefore(endDate)) {
                endDateInRange = endDate;
            }
            dateRanges.add(new LocalDate[]{startDate, endDateInRange});
            startDate = startDate.plusDays(intervalDays);
        }

        return dateRanges.toArray(new LocalDate[0][]);
    }
}
